<template>
  <div class="container">
    <div class="content">
      <div class="title">后台管理系统</div>
      <el-form :model="form" :rules="rules" ref="formRef" label-position="left" label-width="0px" class="demo-form login-container" size="large">
        <el-form-item prop="username" style="margin-top: 5px">
          <el-input type="text" v-model="form.username" auto-complete="off" placeholder="用户名" @keyup.enter="handleSubmit"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="form.password" auto-complete="off" placeholder="密码" @keyup.enter="handleSubmit"></el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button type="primary" style="width:100%;" @click="handleSubmit" :loading="logining">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
  import { ref} from "vue";
  import func from '@/common/func'
  import { useRouter } from 'vue-router';

  const logining = ref(false)
  const form = ref({
    username: '',
    password: '',
  })
  const rules = ref({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
    ]
  })
  const formRef = ref(null)
  const router = useRouter();

  function handleSubmit() {
    formRef.value.validate((valid) => {
      if (!valid) return false
      logining.value = true;
      func.request('/login/login', form.value).then(res => {
        logining.value = false;
        if (res.code === 1) {
          func.setUser(res.data)
          router.push({ path: '/' });
        }
      });
    });
  }

</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100vh;
  background-image: url(../assets/loginbg.png);
  background-size: 100% 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  .title {
    width: 100%;
    text-align: center;
    font-size: 36px;
    color: #fff;
    font-weight: 700;
    letter-spacing: 5px;
  }
  .login-container {
    margin: 30px auto 0;
    width: 300px;
    max-width: 80%;
    height: 160px;
    padding: 30px;
    border-radius: 5px;
    background: hsla(0,0%,100%,.7);
  }
}
</style>